//! # DataForge 🔥🧪
//! 
//! 高性能数据锻造工坊 - 为Rust开发者打造的随机数据生成与数据库填充解决方案
//! 
//! ## 特性
//! 
//! - **闪电般快速** - 基于Rust的高性能随机数生成引擎
//! - **数据库全支持** - 自动生成关系型数据库测试数据
//! - **数据锻造车间** - 200+ 内置数据规则，支持多语言数据
//! 
//! ## 快速开始
//! 
//! ```rust
//! use dataforge::generators::*;
//! use serde_json::json;
//! 
//! // 生成测试用户数据
//! let user = json!({
//!     "id": uuid_v4(),
//!     "name": name::zh_cn_fullname(),
//!     "age": 25,
//!     "email": internet::email(),
//!     "created_at": datetime::iso8601()
//! });
//! ```

pub mod generators;
pub mod regions;
pub mod macros;
pub mod error;
pub mod core;
pub mod memory;
pub mod rules;
pub mod config;
pub mod multithreading;
pub mod customization;
pub mod filling;
pub mod generation;

// 数据库相关模块统一放在 db 下
pub mod db;

// 重新导出核心类型
pub use generators::*;
pub use regions::*;
pub use error::*;
pub use core::{CoreEngine, GenConfig, GenerationStrategy, GeneratorMetrics};
pub use db::{DataType, DatabaseForge, FieldSchema, SchemaParser, SimpleDatabaseForge, TableSchema};
pub use memory::{MemoryPool, MemoryPoolConfig, StringPool};
pub use rules::{Rule, RuleContext, RuleEngine, RuleType};
pub use config::{Config, ConfigManager, DataGenConfig, DatabaseConfig};
pub use multithreading::{ParallelGenerator, ThreadPool};
pub use filling::{ConnectionConfig, ConnectionManager, DatabaseFiller, FillingConfig, FillingStats};
pub use filling::utils;

// 数据库相关类型别名
#[cfg(feature = "database")]
pub use db::{MySqlForge, PostgresForge, SqliteForge};

/// 数据生成器注册表
#[derive(Default)]
pub struct DataForge {
    generators: std::collections::HashMap<&'static str, Box<dyn Fn() -> serde_json::Value + Send + Sync>>,
    #[allow(dead_code)]
    lang: Language,
}

/// 支持的语言
#[derive(Debug, Clone, Copy, Default)]
pub enum Language {
    #[default]
    ZhCN,
    EnUS,
    JaJP,
}

impl DataForge {
    /// 创建新的数据生成器实例
    pub fn new(lang: Language) -> Self {
        let mut df = Self {
            generators: std::collections::HashMap::new(),
            lang,
        };
        df.register_builtin_generators();
        df
    }

    /// 注册内置生成器
    fn register_builtin_generators(&mut self) {
        self.generators.insert("uuid_v4", Box::new(|| {
            serde_json::Value::String(uuid::Uuid::new_v4().to_string())
        }));
        
        self.generators.insert("zh_cn_name", Box::new(|| {
            serde_json::Value::String(generators::name::zh_cn_fullname())
        }));
        
        self.generators.insert("en_us_name", Box::new(|| {
            serde_json::Value::String(generators::name::en_us_fullname())
        }));
    }

    /// 生成指定类型的数据
    pub fn generate(&self, generator_type: &str) -> Option<serde_json::Value> {
        self.generators.get(generator_type).map(|gen| gen())
    }

    /// 注册自定义生成器
    pub fn register<F>(&mut self, name: &'static str, generator: F)
    where
        F: Fn() -> serde_json::Value + Send + Sync + 'static,
    {
        self.generators.insert(name, Box::new(generator));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dataforge_creation() {
        let forge = DataForge::new(Language::ZhCN);
        assert!(forge.generate("uuid_v4").is_some());
    }

    #[test]
    fn test_custom_generator() {
        let mut forge = DataForge::new(Language::ZhCN);
        forge.register("test_gen", || serde_json::Value::String("test".to_string()));
        
        let result = forge.generate("test_gen");
        assert_eq!(result, Some(serde_json::Value::String("test".to_string())));
    }
}